﻿using System;
using System.Windows;
using System.Windows.Input;
using JiaBao.Services.CustomerService;
using JiaBao.Services.LogService;
using JiaBao.WPF.Framework;

namespace JiaBao.WPF
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login :  IRegistrarWPFForm
    {
        ICustomerService _customerService;
        public Login(ICustomerService customerService)
        {
            _customerService = customerService;
            InitializeComponent();
            txt_Uid.Focus();
        }

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Login_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                lab_Msg.Content = "";

                if (_customerService.Login(txt_Uid.Text.Trim(), txt_Pwd.Text.Trim()))
                    DialogResult = true;

                else
                    lab_Msg.Content = "用户名或密码错误";
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void txt_Uid_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                    txt_Pwd.Focus();
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void txt_Pwd_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                    Btn_Login_Click(null, null);
            }
            catch (Exception ex)
            {
                Log.SaveLog(ex);
                MessageBox.Show(ex.Message);
            }
        }

       
    }
}
